Welcome Guest | Sign in | Register
Standard Library Functions - C Programming Interview Questions and Answers | LucentBlackBoard | LucentBlackBoard.com

Home > Technical Interviews > Computer Science & Engineering > C Programming > Standard Library Functions Questions and Answers

6. How do I determine whether a character is numeric, alphabetic, and so on?

The header file ctype.h defines various functions for determining what class a character belongs to. These consist of the following functions:
Function-Character Class- Returns Nonzero for Characters
isdigit() - Decimal digits - 0-9
isxdigit() - Hexadecimal digits - 0-9, a-f, or A-F
isalnum() - Alphanumerics - 0-9, a-z, or A-Z
isalpha() - Alphabetics - a-z or A-Z
islower() - Lowercase alphabetics - a-z
isupper() - Uppercase alphabetics - A-Z
isspace() - Whitespace - Space, tab, vertical tab, newline, form feed, or carriage return
isgraph() - Nonblank characters - Any character that appears nonblank when printed (ASCII 0x21 through 0x7E)
isprint() - Printable characters - All the isgraph() characters, plus space
ispunct() - Punctuation - Any character in isgraph() that is not in isalnum()
iscntrl() - Control characters - Any character not in isprint() (ASCII 0x00 through 0x1F plus 0x7F)
There are three very good reasons for calling these macros instead of writing your own tests for character classes. They are pretty much the same reasons for using standard library functions in the first place. First, these macros are fast. Because they are generally implemented as a table lookup with some bit-masking magic, even a relatively complicated test can be performed much faster than an actual comparison of the value of the character.
Second, these macros are correct. It's all too easy to make an error in logic or typing and include a wrong character (or exclude a right one) from a test.
Third, these macros are portable. Believe it or not, not everyone uses the same ASCII character set with PC extensions. You might not care today, but when you discover that your next computer uses Unicode rather than ASCII, you'll be glad you wrote code that didn't assume the values of characters in the character set.
The header file ctype.h also defines two functions to convert characters between upper- and lowercase alphabetics. These are toupper() and tolower(). The behavior of toupper() and tolower() is undefined if their arguments are not lower- and uppercase alphabetic characters, respectively, so you must remember to check using islower() and isupper() before calling toupper() and tolower().

7. What is a "locale"?

A locale is a description of certain conventions your program might be expected to follow under certain circumstances. It's mostly helpful to internationalize your program.
If you were going to print an amount of money, would you always use a dollar sign? Not if your program was going to run in the United Kingdom; there, you'd use a pound sign. In some countries, the currency symbol goes before the number; in some, it goes after. Where does the sign go for a negative number? How about the decimal point? A number that would be printed 1,234.56 in the United States should appear as 1.234,56 in some other countries. Same value, different convention. How are times and dates displayed? The only short answer is, differently. These are some of the technical reasons why some programmers whose programs have to run all over the world have so many headaches.
Good news: Some of the differences have been standardized. C compilers support different "locales," different conventions for how a program acts in different places. For example, the strcoll (string collate) function is like the simpler strcmp, but it reflects how different countries and languages sort and order (collate) string values. The setlocale and localeconv functions provide this support.
Bad news: There's no standardized list of interesting locales. The only one your compiler is guaranteed to support is the "C" locale, which is a generic, American English convention that works best with ASCII characters between 32 and 127. Even so, if you need to get code that looks right, no matter where around the world it will run, thinking in terms of locales is a good first step. (Getting several locales your compiler supports, or getting your compiler to accept locales you define, is a good second step.)




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.